package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLaborServiceAndEmploymentPublishListBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.view.ui.adapter.packers.LaborServiceAndEmploymentPublishListAdapter;
import com.sgb.kjwl.viewmodel.packers.LaborServiceAndEmploymentPublishListViewModel;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : younghow
 * @date : 2021/7/6 18:20
 * @desc :
 */
public class LaborServiceAndEmploymentPublishListActivity extends AppActivity {

    ActivityLaborServiceAndEmploymentPublishListBinding mBinding;
    LaborServiceAndEmploymentPublishListAdapter mAdapter;
    LaborServiceAndEmploymentPublishListViewModel mViewModel;
    private boolean isLoadMore;
    private int page;

    @Override
    protected void initView() {
        super.initView();

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_labor_service_and_employment_publish_list);
        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(LaborServiceAndEmploymentPublishListViewModel.class);

        initData();
        initRecyclerView();
        initLiveData();
    }

    private void initData() {
        loadData(false);
    }

    private void initLiveData() {
        mViewModel.loadingLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.showProgressDialog(LaborServiceAndEmploymentPublishListActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));
        mViewModel.successLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.dismissProgressDialog());
        mViewModel.errorLiveData.observe(this, aBoolean -> com.swgk.core.dialog.DialogHelper.dismissProgressDialog());

        mViewModel.mServiceLiveData.observe(this, labourServiceAndEmploymentEntities -> {
            if (isLoadMore) {
                if (labourServiceAndEmploymentEntities != null && labourServiceAndEmploymentEntities.size() > 0) {
                    mAdapter.addDatas(labourServiceAndEmploymentEntities);
                }
                if (labourServiceAndEmploymentEntities != null) {
                    mBinding.recyclerView.setNoMore(labourServiceAndEmploymentEntities.size() < 10);
                }
                mBinding.recyclerView.setVisibility(View.VISIBLE);
            } else {
                mBinding.recyclerView.refreshComplete();
                if (labourServiceAndEmploymentEntities != null && labourServiceAndEmploymentEntities.size() > 0) {
                    mBinding.emptyView.setVisibility(View.GONE);
                    mAdapter.setDatas(labourServiceAndEmploymentEntities);
                    mBinding.recyclerView.smoothScrollToPosition(0);
                    if (labourServiceAndEmploymentEntities.size() < NumberConstant.NUMBER_TEN) {
                        mBinding.recyclerView.setNoMore(true);
                    }
                } else {
                    mAdapter.clearDatas();
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        });

        mViewModel.mDeleteSuccessLiveData.observe(this, aBoolean -> loadData(false));
    }

    private void initRecyclerView() {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new LaborServiceAndEmploymentPublishListAdapter(this, null);
        mBinding.recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClick(new AdapterOnItemClick<LabourServiceAndEmploymentEntity>() {
            @Override
            public void onItemClick(LabourServiceAndEmploymentEntity labourServiceAndEmploymentEntity, int position) {
                ActivityJump.jumpToActivityForResult(LaborServiceAndEmploymentPublishListActivity.this, LaborServiceAndEmploymentPublishListDetailActivity.class, 0, Pair.create("no", labourServiceAndEmploymentEntity));
            }
        });

        mAdapter.setClick(new RecyclerClick<LabourServiceAndEmploymentEntity>() {
            @Override
            public void followClick(LabourServiceAndEmploymentEntity entity) {
                ActivityJump.jumpToActivityForResult(LaborServiceAndEmploymentPublishListActivity.this, LaborServiceAndEmploymentPublishActivity.class, 0, Pair.create("entity", entity));
            }

            @Override
            public void secondClick(LabourServiceAndEmploymentEntity entity) {
                DialogHelper.showDialog(LaborServiceAndEmploymentPublishListActivity.this, "温馨提示", "确定删除该劳务用工信息吗？", "取消", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mViewModel.deleteLabour(entity.getNo());
                    }
                });
            }
        });

        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                ActivityJump.jumpToActivityForResult(LaborServiceAndEmploymentPublishListActivity.this, LaborServiceAndEmploymentPublishActivity.class, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            loadData(false);
        }
    }

    private void loadData(boolean isLoadMore) {
        this.isLoadMore = isLoadMore;
        if (isLoadMore) {
            page++;
        } else {
            page = 1;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("limit", 15);
        map.put("page", page);
        int companyType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (companyType == 0) {
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else if (companyType == 1) {
            map.put("companyNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        } else if (companyType == 2) {
            map.put("teamNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }
        mViewModel.getLabourList(map);
    }
}
